package com.linshengjian.web.exception.handler;

import com.linshengjian.core.BusinessException;
import com.linshengjian.web.model.Result;
import com.linshengjian.web.utils.ResultUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by 林圣坚 on 2016/9/3.
 */
@ControllerAdvice
public class BusinessExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result businessException(BusinessException e) throws Exception {
        String code = e.code();
        if (code == null)
            code = Result.Code.FAIL.getValue();
        return ResultUtils.result(code, e.getMessage());
    }
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result exception(Exception e) throws Exception {
        e.printStackTrace();
        return ResultUtils.result(Result.Code.ERROR.getValue(), "服务器异常错误", e.getMessage());
    }
}
